package com.skyworth.tvos.utils;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

import java.util.List;

public class SkyAppService {
    private Context mContext = null;
    private static SkyAppService instance = null;

    public static SkyAppService getInstance() {
        synchronized (SkyAppService.class) {
            if (instance == null) {
                instance = new SkyAppService();
            }
        }
        return instance;
    }

    public static SkyAppService getInstance(Context context) {
        synchronized (SkyAppService.class) {
            if (instance == null) {
                instance = new SkyAppService(context);
            }
        }
        return instance;
    }

    public SkyAppService() {
    }

    public SkyAppService(Context context) {
        this.mContext = context;
    }

    public boolean getAppExist(String packageName) {
        PackageManager mPackageManager = mContext.getPackageManager();
        Intent intent = mPackageManager.getLaunchIntentForPackage(packageName);
        if (intent == null)
            return false;
        else
            return true;
    }

    public boolean isAppRunning(String pkg) {
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> runningTasks = null;
        try {
            runningTasks = am.getRunningAppProcesses();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (runningTasks != null) {
            for (RunningAppProcessInfo info : runningTasks) {
                if (info.processName.equals(pkg))
                    return true;
            }
        }
        return false;
    }
}
